
// OpenESSOView.cpp : implementation of the COpenESSOView class
//

#include "stdafx.h"
#include "OpenESSO.h"

#include "OpenESSODoc.h"
#include "OpenESSOView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// COpenESSOView

IMPLEMENT_DYNCREATE(COpenESSOView, CListView)

BEGIN_MESSAGE_MAP(COpenESSOView, CListView)
	ON_NOTIFY_REFLECT(LVN_ITEMCHANGED,OnItemchangedListCtrl)
END_MESSAGE_MAP()

// COpenESSOView construction/destruction

COpenESSOView::COpenESSOView()
{
	// TODO: add construction code here

}

COpenESSOView::~COpenESSOView()
{
}

BOOL COpenESSOView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CListView::PreCreateWindow(cs);
}

void COpenESSOView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();
	GetListCtrl().ModifyStyle(0, LVS_REPORT);
	CRect rect;
	GetListCtrl().GetClientRect(&rect);
	int nColInterval = rect.Width()/5;
	GetListCtrl().InsertColumn(0, _T("Login"), LVCFMT_LEFT, nColInterval*2);
	GetListCtrl().InsertColumn(1, _T("Password"), LVCFMT_LEFT, nColInterval);
	GetListCtrl().InsertColumn(2, _T("Enabled"), LVCFMT_LEFT, rect.Width()-4*nColInterval);

	// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().
}

void COpenESSOView::OnRButtonUp(UINT nFlags, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void COpenESSOView::OnContextMenu(CWnd* pWnd, CPoint point)
{
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
}


// COpenESSOView diagnostics

#ifdef _DEBUG
void COpenESSOView::AssertValid() const
{
	CListView::AssertValid();
}

void COpenESSOView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

COpenESSODoc* COpenESSOView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COpenESSODoc)));
	return (COpenESSODoc*)m_pDocument;
}
#endif //_DEBUG

COpenESSOView * COpenESSOView::GetView()
{
    CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);

    CView * pView = pFrame->GetActiveView();

    if ( !pView )
       return NULL;

    // Fail if view is of wrong kind
    // (this could occur with splitter windows, or additional
    // views on a single document
    if ( ! pView->IsKindOf( RUNTIME_CLASS(COpenESSOView) ) )
       return NULL;

    return (COpenESSOView *) pView;
}

// on itelm changed

void COpenESSOView::OnItemchangedListCtrl(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	if ((pNMListView->uChanged & LVIF_STATE) && (pNMListView->uNewState & LVNI_SELECTED))
    {
		COpenESSODoc::GetDocument()->UpdateSelectedAccount();
	}
	*pResult = 0;
}